﻿using Jinndev.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;

namespace Jinndev.Modding {

    /// <summary>
    /// 子配置，可挂载到Config上
    /// </summary>
    public abstract class SubConfig : IConfig {

        /// <summary>
        /// 添加到SubConfig实现类上，标记当此SubConfig类型属于某些指定分类时，如果不存在此SubConfig类型的配置，则会自动创建一个
        /// </summary>
        [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
        public class AutoCreate : Attribute {
            public int[] categories;
            public AutoCreate(params int[] categories) {
                this.categories = categories;
            }
        }

        /// <summary>
        /// 类型是否包含指定分类的AutoCreate
        /// </summary>
        public static bool IsAutoCreate(Type type, int category) {
            AutoCreate autoCreate = CommonUtil.GetAttribute<AutoCreate>(type, true);
            if(autoCreate != null) {
                if(autoCreate.categories == null) {
                    return true;
                }
                return autoCreate.categories.Contains(category);
            }
            return false;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init(Config config) {
            Base = config;
            OnInit();
        }
        
        /// <summary>
        /// 实现类初始化时执行
        /// </summary>
        protected abstract void OnInit();
        
        /// <summary>
        /// 主配置
        /// </summary>
        public Config Base { get; private set; }

        /// <summary>
        /// 整型id，唯一但每次运行会改变
        /// </summary>
        public int Id { get { return Base.id; } }
        
        /// <summary>
        /// 字符串id，唯一且永久不变
        /// </summary>
        public string Sid { get { return Base.sid; } }

        /// <summary>
        /// 分类
        /// </summary>
        public int Category { get { return Base.Category; } }

        /// <summary>
        /// 所属Mod
        /// </summary>
        public Mod Mod { get { return Base.mod; } }

        /// <summary>
        /// 获得指定类型的子配置
        /// </summary>
        public T GetSubConfig<T>() where T : SubConfig {
            if (typeof(T).Equals(GetType())) {
                return this as T;
            }
            return Base.GetSubConfig<T>();
        }

        /// <summary>
        /// 获得指定类型的子配置
        /// </summary>
        public SubConfig GetSubConfig(Type type) {
            if (type.Equals(GetType())) {
                return this;
            }
            return Base.GetSubConfig(type);
        }

        /// <summary>
        /// 是否带有指定类型的子配置
        /// </summary>
        public bool HasSubConfig<T>() where T : SubConfig {
            return Base.HasSubConfig<T>();
        }

        /// <summary>
        /// 是否带有指定类型的子配置
        /// </summary>
        public bool HasSubConfig(Type type) {
            return Base.HasSubConfig(type);
        }

        /// <summary>
        /// 输出json，默认导出所有field，需由子类重写
        /// </summary>
        public virtual JObject ToJson() {
            // 子配置不输出属性
            return JObject.FromObject(this, JsonUtil.GetSerializer(true, true));
        }

        ///// <summary>
        ///// 输出json字符串
        ///// </summary>
        //public virtual string ToJson(bool prettyPrint = true) {
        //    return JsonUtil.ToJsonString(this, prettyPrint);
        //}



    }

}
